package com.bringway.p2p.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.MapUtils;
import org.omg.CORBA.PUBLIC_MEMBER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.w3c.dom.ls.LSInput;

import com.bringway.global.domain.SysUserDetails;
import com.bringway.p2p.dao.P2PUserDao;
import com.bringway.p2p.domain.User;
import com.bringway.p2p.service.P2PUserService;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
/*
 * @author fan
 * @className MyUserServiceImpl
 * @description 用户操作业务逻辑实现类
 * @date 2015年12月17日下午4:04:23
 */
@Service
@Transactional
public class P2PUserServiceImpl implements P2PUserService {
	
	@Autowired
	private P2PUserDao userDao;
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.MyUserService#users()
	 */
	@Override
	public List users(JSONObject parms) {
		return userDao.getAllusers(parms);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#usersAllLimit(net.sf.json.JSONObject)
	 */
	@Override
	public List usersAllLimit(JSONObject parms) {
		return userDao.getAllusersLimit(parms);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#addUser(net.sf.json.JSONObject)
	 */
	@Override
	public boolean addUser(JSONObject parms) {
		boolean bool = false;
		//首先添加user
		userDao.addUser(parms);
		//List<User> list = userDao.getAllusers(parms);
		String userId = parms.optString("ID");
		String roleId = parms.optString("ROLEID");
		String XM = parms.optString("XM");
		userDao.addUserRole(userId, roleId, XM);
		bool = true;
		return bool;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#deleteUser(net.sf.json.JSONObject)
	 */
	@Override
	public boolean deleteUser(JSONObject parms) {
		int i = userDao.deleteUser(parms);
		int j = userDao.deleteUser_Menu(parms);
		int k = userDao.deleteUser_Role(parms);
		return i>0;
	}

	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#updateUser(net.sf.json.JSONObject)
	 */
	@Override
	public boolean updateUser(JSONObject parms) {
		return userDao.updateUser(parms)>0;
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bringway.p2p.service.P2PUserService#resetPassword(net.sf.json.
	 * JSONObject)
	 */
	@Override
	public boolean resetPassword(JSONObject parms) {
		return userDao.resetPassword(parms)>0;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.bringway.p2p.service.P2PUserService#login(java.lang.String, java.lang.String)
	 */
	@Override
	public Map<String,Object> login(JSONObject params) {
		Map<String,Object> loginInfos = new HashMap<String,Object>();
		String msg = null;
		boolean isLogin = false;
		String gyh = params.optString("username");
		int i = userDao.getUserByGyh(gyh);
		if(i>0){
			//continue
			int j = userDao.getUserByGyhAndPassword(params);
			if(j>0){
				msg ="登陆成功！";
				isLogin = true;
			}else{
				msg = "密码错误！";
				isLogin = false;
			}
		}else{
			msg =  "用户名输入错误！";
			isLogin =  false;
		}
		loginInfos.put("msg", msg);
		loginInfos.put("isLogin", isLogin);
		return loginInfos;
	}
	
	
	/**
	 * @方法名称:addUserRole
	 * @方法描述:TODO
	 * @author fan
	 * @date 2015年12月31日下午3:17:05
	 * @param userId
	 * @param roleId
	 * @param XM
	 */
	@Override
	public void addUserRole(String userId, String roleId,String XM) {
		userDao.addUserRole(userId,roleId,XM);
	}

	@Override
	public SysUserDetails login(String gyh) {
		return userDao.login(gyh);
	}

	@Override
	public int getPageCount(String tableName) {
		// TODO Auto-generated method stub
		return userDao.getPageCount(tableName);
	}

	@Override
	public boolean comparePassword(JSONObject parms) {
		return userDao.comparePassword(parms)>0;
	}

	@Override
	public boolean changePassword(JSONObject parms) {
		return userDao.changePassword(parms)>0;
	}

	@Override
	public int getUsersItems(JSONObject parms) {
		return userDao.getUsersItems(parms);
	}

	@Override
	public boolean updateAppUser(JSONObject parms) {
			return userDao.updateAppUser(parms)>0;
	
	}

	@Override
	public List<Map<String, Object>> listJkrInfo(JSONObject params) {
		return userDao.listJkrInfo(params);
	}

	@Override
	public int getJkrInfoCount(JSONObject params) {
		return userDao.getJkrInfoCount(params);
	}

	@Override
	public List<Map<String, Object>> listSqrSjhm() {
		return userDao.listSqrSjhm();
	}

	@Override
	public JSONObject checkUsername(String user_name) {
		JSONObject result=new JSONObject();
		String _regexp_zw = "[A-Za-z0-9_]{4,16}";
		Pattern pattern = Pattern.compile(_regexp_zw);
		Matcher matcher = pattern.matcher(user_name);
		if (matcher.matches()) {
			boolean isBeUsername=userDao.isBeUsername(user_name);
			if (isBeUsername) {
				result.put("res", "1");
				result.put("msg", "用户名可以使用");
			} else {
				result.put("res", "0");
				result.put("msg", "用户名已存在");
			}
		}else {
			result.put("res", "0");
			result.put("msg", "用户名由字母、数字或下划线组成的4到16个字符");
		}
		return result;
	}

	@Override
	public JSONObject checkSjhm(String sjhm) {
		JSONObject result=new JSONObject();
		String _regexp_zw = "((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))[0-9]{8}";
		Pattern pattern = Pattern.compile(_regexp_zw);
		Matcher matcher = pattern.matcher(sjhm);
		if (matcher.matches()) {
			boolean isBeSjhm=userDao.isBeSjhm(sjhm);
			if (isBeSjhm) {
				result.put("res", "1");
				result.put("msg", "手机号码可以使用");
			} else {
				result.put("res", "0");
				result.put("msg", "手机号码已被注册");
			}
		}else {
			result.put("res", "0");
			result.put("msg", "手机号码不合法,必须是11位手机号码!");
		}
		return result;
	}

	@Override
	public JSONObject saveJkrInfo(JSONObject params) {
		JSONObject result=new JSONObject();
		String username=params.optString("yhm");
		String sjhm=params.optString("sjhm");
		String pwdDlmm=params.optString("pwdDlmm");
		String pwdJymm=params.optString("pwdJymm");
		String sqr=params.optString("sqr");
		String sqm=params.optString("sqm");
		String openGyh=params.optString("openGyh");
		//验证用户名
		JSONObject checkUsername=this.checkUsername(username);
		if (MapUtils.isNotEmpty(checkUsername)&&"0".equals(MapUtils.getString(checkUsername, "res"))) {
			return checkUsername;
		}
		//验证手机号码
		JSONObject checkSjhm=this.checkSjhm(sjhm);
		if (MapUtils.isNotEmpty(checkSjhm)&&"0".equals(MapUtils.getString(checkSjhm, "res"))) {
			return checkSjhm;
		}
		this.userDao.deleteDisUser(username, sjhm);
		int isInsertUser=userDao.insertUser(username, sjhm, pwdDlmm, sqm);
		if (isInsertUser>0) {
			int isInsertJbxx=userDao.insertJbxx(username, sjhm, pwdJymm);
			if (isInsertJbxx>0) {
				int isInsertRole=userDao.insertRole(username, sjhm);
				if (isInsertRole>0) {
					int isInsertTgcode=userDao.insertTgcode(username, sjhm);
					if (isInsertTgcode>0) {
						int isInsertJkrLog=userDao.insertJkrLog(username, sqr, openGyh);
						if (isInsertJkrLog>0) {
							result.put("res", "1");
							result.put("msg", "注册成功!");
						} else {
							throw new RuntimeException("插入jkrLog表失败!");
						}
					} else {
						throw new RuntimeException("插入tgCode表失败!");
					}
				} else {
					throw new RuntimeException("插入role表失败!");
				}
			} else {
				throw new RuntimeException("插入jbxx表失败!");
			}
		} else {
			throw new RuntimeException("插入user表失败!");
		}
		return result;
	}
	
}
